package com.project.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
@RequestMapping("test")
public class TestController {

    @RequestMapping("upload")
    public String upload(String userName, MultipartFile face) throws Exception{
        //得到上传文件名称
        String fileName = null;
        if(face.isEmpty() == false) {
            fileName = face.getOriginalFilename();
            //以时间毫秒数重命名文件，防止文件名同名，导致的覆盖
            fileName = System.currentTimeMillis() +
                    fileName.substring(fileName.lastIndexOf("."));

            //将上传文件的二进制数据，写入指定文件
            face.transferTo(new File("E:/J193WEB/upload/picture/" + fileName));
        }

        return "上传成功，用户名："+userName+"  上传文件名："+fileName;
    }
}
